package com;

import com.fz.com.fz.dao.StudentDAO;
import com.fz.entity.Book;
import com.fz.entity.Student;
import com.fz.mapper.BookMapper;
import com.fz.mybatis.MyBatisHelper;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.junit.Test;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by webrx on 2017-08-23.
 */
public class Demo {

    @Test
    public void aasdfasf(){
        MyBatisHelper mh = new MyBatisHelper();
        SqlSessionFactory sf = mh.getSqlSessionFactory();

        SqlSession s1 = sf.openSession();
        SqlSession s2 = sf.openSession();
        System.out.println(s1==s2);
    }


    @Test
    public void sss(){
        StudentDAO sdao = new StudentDAO();
        System.out.println(sdao.deleteAll());
        //System.out.println(sdao.count());

//        Map<String,Object> m = sdao.showById(10);
//        System.out.println(m.get("name"));
//        System.out.println(m.get("score"));
//        System.out.println(m.get("id"));

        //Student stu = sdao.sById(10);
        //System.out.println(stu);
        System.out.println(sdao.show().size());

        for(Map<String,Object> mm : sdao.show()){
            System.out.println(mm.get("name"));
            System.out.println(mm.get("score"));
            System.out.println(mm.get("id"));
        }
//        List<Student> sts = sdao.queryByName("%李%");
//        for(Student s : sts){
//            System.out.println(s.getName());
//        }

       // MyBatisHelper mh = new MyBatisHelper();
       // SqlSession s = mh.getSession();
        //BookMapper bdao = s.getMapper(BookMapper.class);
//        Map<String,Object> map = new HashMap<String,Object>();

        //map.put("name","《java实战》");
        //map.put("price",67);
        //map.put("bdate","2017-8-1");
        //bdao.save(map);

        //bdao.add("《企业项目案例》",100,"2018-9-6");
        //bdao.bookadd("《企业项目案例200》",100);

        //s.commit();
    }

    @Test
    public void ttt() {
        StudentDAO sdao = new StudentDAO();
        int tt = sdao.deleteById(6);
        System.out.println(tt);
        List<Student> sts = sdao.queryall();
        for (Student st : sts) {
            System.out.println(st.getName());
        }

//          SqlSession session = new MyBatisHelper().getSession();
//          StudentMapper sdao = session.getMapper(StudentMapper.class);
//          List<Student> sts = sdao.queryall();
//          for(Student s : sts){
//              System.out.println(s.getName());
//          }
//          session.commit();

//        BookMapper bdao = new MyBatisHelper<BookMapper>(BookMapper.class).getMapper();
//        List<Book> bks = bdao.query();
//        System.out.println(bks.size());
//
//        MyBatisHelper<StudentMapper> mh = new MyBatisHelper<StudentMapper>();
//
//        StudentMapper sdao = mh.getMapper(StudentMapper.class);
//        sdao.deleteById(9);
//        List<Student> sts = sdao.queryall();
//        for(Student st : sts){
//            System.out.println(st.getName());
//        }
//        mh.close();

    }


    @Test
    public void show() {
        SqlSession s = new MyBatisHelper<BookMapper>().getSession();
        BookMapper bdao = s.getMapper(BookMapper.class);


        //数据插入
        Book book = new Book();
        book.setPrice(50);
        book.setName("《html设计》");
        try {
            book.setBdate(new SimpleDateFormat("yyyy-MM-dd").parse("2015-06-20"));
        } catch (ParseException e) {
            e.printStackTrace();
        }
        //bdao.insert(book);


        Book bk = bdao.queryById(3);
        System.out.println(bk.getName());

        //数据查询
        List<Book> bks = bdao.query();
        for (Book b : bks) {
            System.out.println(b.getName());
        }

        s.commit();
    }
}
